package com.hebeu.xwl.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.hebeu.xwl.pojo.Article;
import com.hebeu.xwl.pojo.ArticleList;
import com.hebeu.xwl.pojo.Result;
import com.hebeu.xwl.service.ArticleService;
import com.hebeu.xwl.service.BatchService;
import com.hebeu.xwl.service.impl.UserServiceImpl;
import com.hebeu.xwl.vo.PageVo;
import com.hebeu.xwl.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@SaCheckLogin
@SaCheckRole("user")
@RequestMapping("/user/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private BatchService batchService;

    @PostMapping("/add")
    public Result addArticleList(@RequestBody ArticleList myArticleList){
        List<Article> articleList = myArticleList.getArticleList();
        int batchId = myArticleList.getBatchId();
        int userId = Integer.parseInt(((String) StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        String index = batchService.findIndex(batchId);
        int articleNum =articleService.findArticleNum(index);
        int maxArticleNum = userService.findMAxArticle(userId);
        if( (articleNum + articleList.size()) <= maxArticleNum){
//            try{
                articleService.addArticleList(articleList,index,userId);
                result.setCode(0);
                result.setMsg("success");
//            }catch (Exception e){
//                result.setCode(101);
//                result.setMsg(e.toString());
//            }
        }else if(articleNum < maxArticleNum &&  (articleNum + articleList.size()) > maxArticleNum){
            result.setCode(102);
            result.setMsg("此次插入数据过多，会导致数据量超过预定值，添加失败");
        }else if(articleNum >= maxArticleNum){
            result.setCode(103);
            result.setMsg("此数据集中数据量已达上限，添加失败");
        }

        return result;
    }

    @PostMapping("/overWrite")
    public Result overWrite(@RequestBody ArticleList myArticleList){
        List<Article> articleList = myArticleList.getArticleList();
        int batchId = myArticleList.getBatchId();
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        String index = batchService.findIndex(batchId);
        int maxArticleNum = userService.findMAxArticle(userId);
        if(articleList.size() > maxArticleNum){
            result.setCode(101);
            result.setMsg("传入数据量过大，超过单个数据集储存限制");
        }else {
            articleService.overwrite(articleList,index,userId);
            result.success();
        }
        return result;
    }

    @GetMapping("/findAll")
    public Result<List<Article>> findAll(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        String index = batchService.findIndex(batchId);
        try{
            List<Article> articleList = articleService.findAll(index,userId);
            result.setCode(0);
            result.setMsg("success");
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(101);
            result.setMsg(e.toString());
        }
        return result;
    }

    @GetMapping("/pageFind")
    public Result<PageVo<Article>> pageFind(@RequestParam int batchId
                                        , @RequestParam int page){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);

        Result result = new Result();
        String index = batchService.findIndex(batchId);
        try{
            PageVo<Article> pageVo = articleService.pageFind(index,userId,page);
            result.setData(pageVo);
            result.success();
        }catch (Exception e){
            result.setCode(101);
            result.setMsg(e.toString());
        }
        return result;
    }

    /**
     * 查看数据集中每日的文章发布量
     * @param batchId
     * @return
     */
    @GetMapping("/countByPubdate")
    public Result<String> countByPubdate(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.countByPubdate(userId,batchId);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 指定数据集中指定用户的每日文章发布量
     * @param batchId
     * @param author
     * @return
     */
    @GetMapping("/countByPubdateAndAuthor")
    public Result<String> countByPubdateAndAuthor(@RequestParam int batchId,
                                          @RequestParam String author){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.countByPubdateAndAuthor(userId,batchId,author);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 指定数据集中各个关键字出现的次数
     * @param batchId
     * @return
     */
    @GetMapping("/keywordsCount")
    public Result<String> keywordsCount(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.keywordsCount(userId,batchId);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 指定数据集中指定作者发布的文章的各个关键字出现的次数
     * @param batchId
     * @param author
     * @return
     */
    @GetMapping("/keywordsCountByAuthor")
    public Result<String> keywordsCountByAuthor(@RequestParam int batchId,
                                        @RequestParam String author){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.keywordsCountByAuthor(userId,batchId,author);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查询执行数据集中各个作者的点赞量
     * @param batchId
     * @return
     */
    @GetMapping("/sumLikesByAuthor")
    public Result<String> sumLikesByAuthor(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.sumLikesByAuthor(userId,batchId);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查询执行数据集中各个作者的浏览量
     * @param batchId
     * @return
     */
    @GetMapping("/sumViewsByAuthor")
    public Result<String> sumViewsByAuthor(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.sumViewsByAuthor(userId,batchId);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查询执行数据集中各个作者的评论量
     * @param batchId
     * @return
     */
    @GetMapping("/sumCommentaryByAuthor")
    public Result<String> sumCommentaryByAuthor(@RequestParam int batchId){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            String data = articleService.sumCommentaryByAuthor(userId,batchId);
            result.success();
            result.setData(data);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;

    }


    /**
     * 查看指定数据集中指定日期的文章数据
     * @param batchId
     * @param pubdate
     * @return
     */
    @GetMapping("/findArticleByPubdate")
    public Result<List<Article>> findArticleByPubdate(@RequestParam int batchId,
                                                      @RequestParam String pubdate){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            List<Article> articleList =
                    articleService.findArticleByPubdate
                            (userId,batchId,pubdate);
            result.success();
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查看指定数据集中指定作者在指定日期发布的文章数据
     * @param batchId
     * @param pubdate
     * @param author
     * @return
     */
    @GetMapping("/findArticleByPubdateAndAuthor")
    public Result<List<Article>> findArticleByPubdateAndAuthor(
            @RequestParam int batchId,
            @RequestParam String pubdate,
            @RequestParam String author){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            List<Article> articleList =
                    articleService.findArticleByPubdateAndAuthor
                            (userId,batchId,pubdate,author);
            result.success();
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查看指定数据集中指定作者的文章
     * @param batchId
     * @param author
     * @return
     */
    @GetMapping("/findArticleByAuthor")
    public Result<List<Article>> findArticleByAuthor( @RequestParam int batchId,
                                                      @RequestParam String author){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            List<Article> articleList =
                    articleService.findArticleByAuthor(userId,batchId,author);
            result.success();
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查看指定数据集中有关指定关键字的文章
     * @param batchId
     * @param keywords
     * @return
     */
    @GetMapping("/findArticleByKeywords")
    public Result<List<Article>> findArticleByKeywords(@RequestParam int batchId,
                                                       @RequestParam String keywords){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            List<Article> articleList =
                    articleService.findArticleByKeywords(userId,batchId,keywords);
            result.success();
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 查看指定数据集中指定作者发布的有关指定关键字的文章数据
     * @param batchId
     * @param keywords
     * @param author
     * @return
     */
    @GetMapping("/findArticleByKeywordsAndAuthor")
    public Result<List<Article>> findArticleByKeywordsAndAuthor(
            @RequestParam int batchId,
            @RequestParam String keywords,
            @RequestParam String author){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        try{
            List<Article> articleList =
                    articleService.findArticleByKeywordsAndAuthor
                            (userId,batchId,keywords,author);
            result.success();
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

    /**
     * 根据指定条件组合查找指定数据集中的文章数据
     * @param batchId
     * @param author    不必要
     * @param pubdate   不必要
     * @param keywords  不必要
     * @return
     */
    @GetMapping ("/findArticleBySearchVo")
    public Result<List<Article>> findArticleBySearchVo(
            @RequestParam int batchId,
            @RequestParam(required = false) String author ,
            @RequestParam(required = false) String pubdate,
            @RequestParam(required = false) String keywords
            ){
        int userId = Integer.parseInt(((String)StpUtil.getLoginId()).split("%")[0]);
        Result result = new Result();
        SearchVo searchVo = new SearchVo();
        if(author != null){
            searchVo.setAuthor(author);
        }
        if(pubdate != null){
            searchVo.setPubdate(pubdate);
        }
        if(keywords != null){
            searchVo.setKeywords(keywords);
        }
        try{
            List<Article> articleList =
                    articleService.findArticleBySearchVo(userId,batchId,searchVo);
            result.success();
            result.setData(articleList);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("error");
        }
        return result;
    }

}
